<?php
/**
 * #Celebrate Shieh
 *             ,%%%%%%%%,
 *           ,%%/\%%%%/\%%
 *          ,%%%\c "" J/%%%
 * %.       %%%%/ o  o \%%%
 * `%%.     %%%%    _  |%%%
 *  `%%     `%%%%(__Y__)%%'
 *  //       ;%%%%`\-/%%%'
 * ((       /  `%%%%%%%'
 *  \\    .'          |
 *   \\  /       \  | |
 *    \\/         ) | |
 *     \         /_ | |__
 *     (___________))))))) 攻城湿
 *
 *        _       _
 * __   _(_)_   _(_) __ _ _ __
 * \ \ / / \ \ / / |/ _` |'_ \
 *  \ V /| |\ V /| | (_| | | | |
 *   \_/ |_| \_/ |_|\__,_|_| |_|
 */

namespace App\Services;

use App\Models\Blog\Article;
use App\Models\Blog\Feedback;
use App\Models\Blog\Gallery;
use App\Models\System\File;
use App\User;
use Illuminate\Http\Request;

class SiteService
{
    use Service;

    /**
     * 回收站首页
     *
     * @param Request $request
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function SiteIndexService( $request )
    {
        if ( ! $request->filled('status') ) request()->offsetSet('status', 'article');
        switch ( $request->input('status') ) {
            case 'article':
                $select = [
                    'id', 'user_id', 'category_id', 'parent_id', 'title', 'is_show', 'deleted_at', 'like', 'share',
                    'browse', 'step', 'comment', 'root', 'status', 'fail', 'pic', 'is',
                ];
                $with   = [
                    'user'         => function ( $query ) {
                        $query->select('id', 'name');
                    },
                    'category'     => function ( $query ) {
                        $query->select('id', 'name');
                    },
                    'parent'       => function ( $query ) {
                        $query->select('id', 'name');
                    },
                    'articleLabel' => function ( $query ) {
                        $query->select('id', 'name');
                    },
                    'file'         => function ( $query ) {
                        $query->select('id', 'url');
                    },
                ];
                $site   = Article::PaginateModel([ 'static' => 3 ], $with, $select);
                break;
            case 'user':
                $select = [ 'id', 'name', 'email', 'phone', 'is_show', 'static', 'created_at' ];
                $with   = [
                    'info' => function ( $query ) {
                        $query->select('id', 'uid', 'rename', 'avatar', 'content');
                    },
                ];
                $site   = User::PaginateModel([ 'status' => 3, 'is_show' => 1 ], $with, $select);
                break;
            case 'blogUser':
                $with   = [
                    'info' => function ( $query ) {
                        $query->select('uid', 'rename', 'username', 'card', 'content', 'avatar', 'constellation', 'github', 'sex',
                            'province', 'city', 'area', 'code', 'frequency', 'openid', 'birthday', 'zodiac');
                        $query->with([
                            'provinceAll' => function ( $query ) {
                                $query->select('id', 'name');
                            },
                        ]);
                        $query->with([
                            'cityAll' => function ( $query ) {
                                $query->select('id', 'name');
                            },
                        ]);
                        $query->with([
                            'areaAll' => function ( $query ) {
                                $query->select('id', 'name');
                            },
                        ]);
                        $query->with([
                            'file' => function ( $query ) {
                                $query->select('id', 'url', 'file_url');
                            },
                        ]);
                    },
                ];
                $select = [ 'id', 'name', 'email', 'phone', 'is_show', 'static', 'deleted_at' ];
                $site   = User::PaginateModel([ 'status' => 3, 'is_show' => 2 ], $with, $select);
                foreach ( $site as &$value ) {
                    $region = '';
                    if ( isset($value->info->provinceAll->name) ) {
                        $region .= $value->info->provinceAll->name . '-';
                    }
                    if ( isset($value->info->cityAll->name) ) {
                        $region .= $value->info->cityAll->name . '-';
                    }
                    if ( isset($value->info->areaAll->name) ) {
                        $region .= $value->info->areaAll->name;
                    }
                    $region = ltrim($region, '-');
                    $region = rtrim($region, '-');
                    if ( ! $region ) $value->region = '暂无地址';
                    else $value->region = $region;
                    unset($value->info->provinceAll, $value->info->cityAll, $value->info->areaAll);
                }
                break;
            case 'feedback':
                $select = [
                    'id', 'category_id', 'parent_id', 'user_id', 'status', 'updated_at', 'phone', 'content', 'ip',
                    'country', 'area', 'region', 'city', 'county', 'fail', 'static',
                ];
                $where  = function ( $query ) use ( $request ) {
                    $query->where('is_show', 3);
                    $query->where('status', 1);
                };
                $with   = [
                    'user'     => function ( $query ) {
                        $query->select('id', 'name');
                    },
                    'category' => function ( $query ) {
                        $query->select('id', 'name');
                    },
                    'parent'   => function ( $query ) {
                        $query->select('id', 'name');
                    },
                    'pic'      => function ( $query ) {
                        $query->select('id', 'url', 'file_url', 'filename');
                    },
                ];
                $site   = Feedback::GetModel($where, $with, $select);
                break;
            case 'gallery':
                $with   = [
                    'user'     => function ( $query ) {
                        $query->select('id', 'name');
                    },
                    'category' => function ( $query ) {
                        $query->select('id', 'name');
                    },
                    'file'     => function ( $query ) {
                        $query->select('id', 'url');
                    },
                ];
                $where  = function ( $query ) use ( $request ) {
                    $query->where('static', 3);
                };
                $select = [
                    'id', 'user_id', 'category_id', 'parent_id', 'title', 'is_show', 'created_at', 'like', 'share',
                    'browse', 'step', 'comment', 'root', 'status', 'fail', 'pic', 'is',
                ];
                $site   = Gallery::PaginateModel($where, $with, $select);
                break;
            case 'file':
                $where  = function ( $query ) use ( $request ) {
                    $query->where('status', 3);
                };
                $with   = [
                    'user'    => function ( $query ) {
                        $query->select('id', 'name');
                    },
                    'primary' => function ( $query ) {
                        $query->select('url', 'file_id', 'file_url');
                    },
                ];
                $select = [
                    'id', 'size', 'name', 'hash', 'file_url', 'url', 'file_url', 'created_at', 'ext', 'uid', 'category',
                    'user',
                ];
                $site   = File::PaginateModel($where, $with, $select);
                break;
            default:
                return back()->with('error', $this->getErrorCode()::message(1001));
                break;
        }
        $site->appends([
            'status' => $request->input('status'),
        ])->render();
        return view('expansion.site.index', compact('site', 'request'));
    }

    /**
     * 还原
     *
     * @param Request $request
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function SiteReductionService( $request )
    {
        if ( ! is_array($request->input('id')) ) return message($this->getErrorCode()::message(1000));
        $where = function ( $query ) use ( $request ) {
            $query->whereIn('id', $request->input('id'));
        };
        switch ( $request->input('status') ) {
            case 'article':
                $data[ 'static' ] = $request->input('static', 1);
                if ( $request->input('static', 1) != 2 ) $data[ 'deleted_at' ] = NULL;
                Article::UpdateModel($where, $data);
                break;
            case 'feedback':
                $data[ 'is_show' ] = $request->input('static', 1);
                if ( $request->input('static', 1) != 2 ) $data[ 'deleted_at' ] = NULL;
                Feedback::UpdateModel($where, $data);
                break;
            case 'gallery':
                $data[ 'static' ] = $request->input('static', 1);
                if ( $request->input('static', 1) != 2 ) $data[ 'deleted_at' ] = NULL;
                Gallery::UpdateModel($where, $data);
                break;
            case 'file':
                $data[ 'status' ] = $request->input('static', 1);
                if ( $request->input('static', 1) != 2 ) $data[ 'deleted_at' ] = NULL;
                File::UpdateModel($where, $data);
                break;
            default:
                $data[ 'status' ] = $request->input('static', 1);
                if ( $request->input('static', 1) != 2 ) $data[ 'deleted_at' ] = NULL;
                User::UpdateModel($where, $data);
                break;
        }
        return data(NULL, '还原成功');
    }
}
